package jpb.util.load.excel;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by haol on 2017/5/10.
 */
public class ParseExcel {

        public static <T> void parseExcel(String[] columns, String[] properties,
                                          Class[] classes, List<T> data, Class<T> clz,String head[]) throws Exception {
                try {
                        T instance = clz.newInstance();
                        for(int propIndex = 0; propIndex < properties.length; propIndex++) {
                                String property = properties[propIndex];
                                String setMethodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
                                Method setMethod = clz.getMethod(setMethodName, classes[propIndex]);
                                String columnValue = head[propIndex];
                                if(classes[propIndex] == BigDecimal.class) {
                                        setMethod.invoke(instance, new BigDecimal(columnValue));
                                } else if (classes[propIndex] == Integer.class){
                                        setMethod.invoke(instance, new Integer(columnValue));
                                } else if (classes[propIndex] == Double.class) {
                                        setMethod.invoke(instance, new Double(columnValue));
                                } else {
                                        setMethod.invoke(instance, columnValue);
                                }
                        }
                        data.add(instance);
                } catch (Exception ex) {
                        ex.printStackTrace();
                        throw ex;
                        //看是否需要记录哪一行产生的错误.
                }
        }

}
